<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2024/3/11
 * Time: 11:23
 */

namespace app\common\helper;


use app\common\enum\ConfigEnum;
use yii\db\Expression;

class TreeMapItem
{
    public $tree_class;
    public $leaf_class;
    public $leaf_field_id = "id";
    public $leaf_field_name = "mc";
    public $leaf_field_map;
    public $leaf_where;

    public $tree_field_id = "id";
    public $tree_field_name = "mc";
    public $tree_field_pid = "pid";
    public $tree_where ;

    public $leaf_soft_field ;
    public $tree_soft_field ;

    public $ext_server;

    /**
     * @return mixed
     */
    public function getExtServer()
    {
        return $this->ext_server;
    }

    /**
     * @param $ext_server
     * @return $this
     */
    public function setExtServer($ext_server)
    {
        $this->ext_server = $ext_server;
        return $this;
    }


    /**
     * @return mixed
     */
    public function getLeafWhere()
    {
        return $this->leaf_where;
    }

    /**
     * @param $leaf_where
     * @return $this
     */
    public function setLeafWhere($leaf_where)
    {
        $this->leaf_where = $leaf_where;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getTreeWhere()
    {
        return $this->tree_where;
    }

    /**
     * @param $tree_where
     * @return $this
     */
    public function setTreeWhere($tree_where)
    {
        $this->tree_where = $tree_where;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getTreeClass()
    {
        return $this->tree_class;
    }

    /**
     * @param $tree_class
     * @return $this
     */
    public function setTreeClass($tree_class)
    {
        $this->tree_class = $tree_class;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getLeafClass()
    {
        return $this->leaf_class;
    }

    /**
     * @param $leaf_class
     * @return $this
     */
    public function setLeafClass($leaf_class)
    {
        $this->leaf_class = $leaf_class;
        return $this;
    }

    /**
     * @return string
     */
    public function getLeafFieldId()
    {
        return $this->leaf_field_id;
    }

    /**
     * @param $leaf_field_id
     * @return $this
     */
    public function setLeafFieldId($leaf_field_id)
    {
        $this->leaf_field_id = $leaf_field_id;
        return $this;
    }

    /**
     * @return string
     */
    public function getLeafFieldName()
    {
        return $this->leaf_field_name;
    }

    /**
     * @param $leaf_field_name
     * @return $this
     */
    public function setLeafFieldName($leaf_field_name)
    {
        $this->leaf_field_name = $leaf_field_name;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getLeafFieldMap()
    {
        return $this->leaf_field_map;
    }

    /**
     * @param $leaf_field_map
     * @return $this
     */
    public function setLeafFieldMap($leaf_field_map)
    {
        $this->leaf_field_map = $leaf_field_map;
        return $this;
    }

    /**
     * @return string
     */
    public function getTreeFieldId()
    {
        return $this->tree_field_id;
    }

    /**
     * @param $tree_field_id
     * @return $this
     */
    public function setTreeFieldId($tree_field_id)
    {
        $this->tree_field_id = $tree_field_id;
        return $this;
    }

    /**
     * @return string
     */
    public function getTreeFieldName()
    {
        return $this->tree_field_name;
    }

    /**
     * @param $tree_field_name
     * @return $this
     */
    public function setTreeFieldName($tree_field_name)
    {
        $this->tree_field_name = $tree_field_name;
        return $this;
    }

    /**
     * @return string
     */
    public function getTreeFieldPid()
    {
        return $this->tree_field_pid;
    }

    /**
     * @param $tree_field_pid
     * @return $this
     */
    public function setTreeFieldPid($tree_field_pid)
    {
        $this->tree_field_pid = $tree_field_pid;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getLeafSoftField()
    {
        return $this->leaf_soft_field;
    }

    /**
     * @param $leaf_soft_field
     * @return $this
     */
    public function setLeafSoftField($leaf_soft_field)
    {
        $this->leaf_soft_field = $leaf_soft_field;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getTreeSoftField()
    {
        return $this->tree_soft_field;
    }

    /**
     * @param $tree_soft_field
     * @return $this
     */
    public function setTreeSoftField($tree_soft_field)
    {
        $this->tree_soft_field = $tree_soft_field;
        return $this;
    }


    public static function createInstance(){
        return new static();
    }


    public function getOptions(){
        $tree_class = $this->getTreeClass();
        $leaf_class = $this->getLeafClass();
        $leaf_query = $leaf_class::find()->select([
            new Expression($this->getLeafFieldId()),
            new Expression($this->getLeafFieldName()),
            new Expression($this->getLeafFieldMap()),
            new Expression($this->getLeafFieldId()." as value"),
            new Expression($this->getLeafFieldName()." as title"),
        ]);
        if($where = $this->getLeafWhere()){
            $leaf_query->andWhere($where);
        }
        if($this->getLeafSoftField()){
            $leaf_query->orderBy($this->getLeafSoftField());
        }
        $leaf_data = $leaf_query->asArray()->all();


        $tree_query = $tree_class::find()->select([
            new Expression($this->getTreeFieldId()),
            new Expression($this->getTreeFieldName()),
            new Expression($this->getTreeFieldPid()),
            new Expression($this->getTreeFieldId()." as value"),
            new Expression($this->getTreeFieldName()." as title"),
        ]);
        if($where = $this->getTreeWhere()){
            $tree_query->andWhere($where);
        }
        if($this->getTreeSoftField()){
            $tree_query->orderBy($this->getTreeSoftField());
        }
        $tree_data = $tree_query->asArray()->all();
        $tree = [];
        if($tree_data){
            if($ext_class = $this->getExtServer()){
                foreach ($tree_data as $k => $tree){
                    $tree_data[$k][ConfigEnum::EXTEND_CONFIG] = (new $ext_class())->getConfigById($tree[$this->getTreeFieldId()]);
                }
            }
            $tree = TreeHelper::generateTree($tree_data);
            $tree = TreeHelper::addTreeLeaf($tree,$leaf_data,$this->getTreeFieldId(),$this->getLeafFieldMap());
        }



        return $tree;
    }
}